package com.yxinsur.imc.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.enums.OrderStatusCodeEnum;
import com.yxinsur.imc.business.form.OrderRiskInputForm;
import com.yxinsur.imc.common.exception.BusinessException;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.validator.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcOrderRiskMapper;
import com.yxinsur.imc.business.domain.ImcOrderRisk;
import com.yxinsur.imc.business.service.IImcOrderRiskService;

/**
 * 保单险种详情Service业务层处理
 *
 * @author imc
 * @date 2021-03-18
 */
@Service
public class ImcOrderRiskServiceImpl extends ServiceImpl<ImcOrderRiskMapper, ImcOrderRisk> implements IImcOrderRiskService
{
    @Autowired
    private ImcOrderRiskMapper imcOrderRiskMapper;

    /**
     * 修改续期险种信息
     *
     * @param risk 险种信息
     * @return
     */
    @Override
    public int renewalRiskEdit(OrderRiskInputForm risk) {
        ImcOrderRisk orderRisk = this.imcOrderRiskMapper.selectById(risk.getId());
        String isCommPaid = orderRisk.getIsCommPaid();
        String isFeeSettled = orderRisk.getIsFeeSettled();
        if("Y".equals(isCommPaid)){
            throw new BusinessException("佣金已发放，不允许修改数据");
        }
        if("Y".equals(isFeeSettled)){
            throw new BusinessException("手续费已结算，不允许修改数据");
        }
        Assert.isNull(orderRisk,"请求数据有误，请稍后再试");
        BeanUtils.copyProperties(risk,orderRisk);
        orderRisk.setUpdateId(SecurityUtils.getUsername());
        orderRisk.setUpdateTime(DateUtils.getNowDate());
        return this.imcOrderRiskMapper.updateById(orderRisk);
    }

    @Override
    public int renewalAdd(OrderRiskInputForm risk) {
        ImcOrderRisk imcOrderRisk = new ImcOrderRisk();
        BeanUtils.copyProperties(risk,imcOrderRisk);

        imcOrderRisk.setRiskStatusCode(OrderStatusCodeEnum.STATUS_20.getCode());
        //续期折标率默认为1
        imcOrderRisk.setStandRate(risk.getStandRate()==null?new BigDecimal(1L):risk.getStandRate());
        imcOrderRisk.setCreateId(SecurityUtils.getUsername());
        imcOrderRisk.setCreateTime(DateUtils.getNowDate());
        imcOrderRisk.setUpdateId(SecurityUtils.getUsername());
        imcOrderRisk.setUpdateTime(DateUtils.getNowDate());
        imcOrderRisk.setTenantId(SecurityUtils.getTenantId());
        return imcOrderRiskMapper.insert(imcOrderRisk);
    }

    @Override
    public void renewalDelete(Long orderRiskId) {
        ImcOrderRisk imcOrderRisk = new ImcOrderRisk();
        imcOrderRisk.setId(orderRiskId);
        imcOrderRisk.setIsDeleted(String.valueOf(orderRiskId));
        imcOrderRiskMapper.updateById(imcOrderRisk);
    }
}
